angular.module('main', ['ui.bootstrap.modal','ngMaterial']).controller(
		'mainCtrl', [ '$scope', '$http', 'appService', '$modal', function($scope, $http, appService, $modal) {
    $scope.syncData = function(){
        $http.get('api/dda/ratekey/sync').success(function(data){
        	if (data.status != null && data.status == 'ERROR') {
        		appService.showStatus("error", data.msg, 3000);
        	} else {
	            $scope.rateKeyList = data;
	            initDataTable($scope.rateKeyList);
	            console.log("wrapRateKey list has been loaded");
        	}
        });
    }

    $scope.getData = function(){
        $http.get('api/dda/ratekey/get').success(function(data){
            $scope.rateKeyList = data;
            initDataTable($scope.rateKeyList);
            console.log("wrapRateKey list has been loaded");
        });
    }

    $scope.loadSingleRateKeySubList = function(rateKey){
        $scope.rateKeyAddList = rateKey.rateKeyAddList;
    }
    
    $scope.addNew = function() {
    	var blankRateKey = {
    			wrapKeyId:"",
    			wrapInsLineCd:"",
    			wriKeyDes:"",
    			wriFormLevelCd:""
    	};
    	var wrapKeyTable = $('#wrapRateKey').DataTable();
    	loadPopup(wrapKeyTable, blankRateKey, 0, 'insert');
    }
    
    function initDataTable(data) {
    	$('#wrapRateKey tbody').off( 'click', 'tr');
    	var tableData = [];
        angular.forEach(data, function(value,index){
            tableData.push([
					value.wrapKeyId,
					value.wrapInsLineCd,
					value.wriKeyDes,
					value.wriFormLevelCd 
				]);
        });
        var wrapKeyTable = $('#wrapRateKey').DataTable({
        	data: tableData,
        	sAutoWidth: false,
        	aoColumns: [
        	              { "sWidth": "20%" },
						  { "sWidth": "20%" },
						  { "sWidth": "40%" },
						  { "sWidth": "20%" }],
            destroy: true,
            fnInitComplete: function() {
            	$('#wrapRateKey tbody').on( 'click', 'tr', function() {
            		var index = wrapKeyTable.row( this ).index();
            		var rateKey = $scope.rateKeyList[index];
                	loadPopup(wrapKeyTable, rateKey, index, 'update');
                });
            } 
        });
    }
    
    function loadPopup(wrapKeyTable, item, index, action) {
        var modalInstance = $modal.open({
            templateUrl: 'views/myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: 'lg',
            resolve: {
                item: function () {
                    return {item : item, index : index, action : action};
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
        	if (selectedItem.error) {
        		return;
        	}
        	var wrapKeyTable = $('#wrapRateKey').dataTable();
        	var item = selectedItem.item;
        	var action = selectedItem.action;
        	var index = selectedItem.index;
        	if (action == 'update') {
        		$scope.rateKeyList[index] = item;
        	} else if (action == 'delete') {
        		$scope.rateKeyList.splice(index, 1);
        	} else if (action = 'insert') {
        		$scope.rateKeyList.push(item);
        	}
        	initDataTable($scope.rateKeyList);
        }, function () {
            console.log('Modal dismissed at: ' + new Date());
        });
    };
    
    $scope.getData();
    
}]);


angular.module('ui.bootstrap.demo', ['ui.bootstrap.modal'])
    .controller('ModalInstanceCtrl', [ '$scope', '$http', '$modalInstance', 'item', 
                                       function ($scope, $http, $modalInstance, item) {

  $scope.item = item.item;
  $scope.action = item.action
  
  $scope.ok = function () {
      $http.post('/api/dda/ratekey/' + $scope.action, $scope.item).success(function(data){
          console.log("update success");
          console.log(data);
          $modalInstance.close({item:$scope.item,index:item.index, action:$scope.action});
      }).error(function(data){
    	  console.log('Error');
    	  console.log(data);
    	  $modalInstance.close({error:"error"});
      });
  };
  
  $scope.del = function() {
	  $http.post('/api/dda/ratekey/delete', $scope.item).success(function(data){
          console.log("delete success");
          console.log(data);
      });
	  $modalInstance.close({item:$scope.item,index:item.index, action:'delete'});
  }

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
}]);

